package com.twu.biblioteca.model.menu;

import com.twu.biblioteca.model.Customer;
import com.twu.biblioteca.model.Library;

import java.util.ArrayList;
import java.util.List;

public class Menu {

    private final Library library;
    private Customer loggedInCustomer;
    private boolean running;

    public Menu(Library library) {
        this.library = library;
        running = false;
    }

    public List<Command> getValidCommands() {
        List<Command> available = new ArrayList<>();

        for (Command command : Command.values()) {
            if (command.getAction().isNeedsLogin() == isUserLoggedIn() ||
                    !command.getAction().isNeedsLogin())
                available.add(command);
        }
        return available;
    }

    public boolean selectCommand(Command command, String[] params) {
        return (command != null) && command.getAction().performAction(this, params);
    }

    public Customer getLoggedInCustomer() {
        return loggedInCustomer;
    }

    public void setLoggedInCustomer(Customer loggedInCustomer) {
        this.loggedInCustomer = loggedInCustomer;
    }

    public boolean isUserLoggedIn() {
        return loggedInCustomer != null;
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public Library getLibrary() {
        return library;
    }
}
